package com.meiqiongproperty.yiguanjia.fragment.home.tenement;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.RepaircomplaintLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.user.LoginDetailFragment;
import com.meiqiongproperty.yiguanjia.fragment.user.TelephoneLoginFragment;

import java.util.ArrayList;

/**
 * 报修投诉
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.home.tenement
 * @User ChenJing
 * @Date 2017/5/7
 */

public class RepairComplaintFragment extends BaseFragment {
    RepaircomplaintLayoutBinding binding;
    private ArrayList<Fragment> fragmentsList;
    MyFragmentPagerAdapter pagerAdapter;
    ComplaintFragment leftFramgnet;
    BXDFragment rightFramgnet;
    int index=0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater, R.layout.repaircomplaint_layout,container,false);
        binding.setClick(this);
        initView();
        return binding.getRoot();
    }

    private void initView() {
        binding.navigation.setTitle("报修投诉");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        fragmentsList = new ArrayList<Fragment>();
        leftFramgnet = new ComplaintFragment();
        rightFramgnet = new BXDFragment();
        fragmentsList.add(leftFramgnet);
        fragmentsList.add(rightFramgnet);
        pagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(),
                fragmentsList);
        binding.viewPager.setAdapter(pagerAdapter);
        binding.viewPager.setOffscreenPageLimit(0);
        binding.viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        binding.viewPager.setCurrentItem(0);
    }

    /**
     * 页面变化监听
     *
     * @author Administrator
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    index=0;
                    binding.leftText.setTextColor(getResources().getColor(R.color.black));
                    binding.rightText.setTextColor(getResources().getColor(R.color.gray));
                    binding.informationLine.setVisibility(View.VISIBLE);
                    binding.sysLine.setVisibility(View.INVISIBLE);
                    break;
                case 1:
                    index=1;
                    binding.informationLine.setVisibility(View.INVISIBLE);
                    binding.sysLine.setVisibility(View.VISIBLE);
                    binding.leftText.setTextColor(getResources().getColor(R.color.gray));
                    binding.rightText.setTextColor(getResources().getColor(R.color.black));
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    public void left_tab_click(View view) {
        binding.viewPager.setCurrentItem(0);
    }

    public void right_tab_click(View view) {
        binding.viewPager.setCurrentItem(1);
    }

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<Fragment> fragmentsList;
        boolean flag = false;
        private FragmentManager fm;

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public MyFragmentPagerAdapter(FragmentManager fm,
                                      ArrayList<Fragment> fragments) {
            super(fm);
            this.fragmentsList = fragments;
            this.fm = fm;
        }

        @Override
        public int getCount() {
            return fragmentsList.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            return fragmentsList.get(arg0);
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public Object instantiateItem(View container, int position) {
            // 得到缓存的fragment
            Fragment fragment = (Fragment) super.instantiateItem(container,
                    position);
//		FragmentManager fm = getChildFragmentManager();
            // 得到tag，这点很重要
            String fragmentTag = fragment.getTag();
            // 因为固定要更新第二个fragment，所以position固定写成1，而flag是我写的更新的标记，默认为false，如果要更新，则置为true
            if (flag && position == 0) {
                // 如果这个fragment需要更新
                FragmentTransaction ft = fm.beginTransaction();
                // 移除旧的fragment
                ft.remove(fragment);
                // 换成新的fragment，定义成你想要的新的fragment
                fragment = new LoginDetailFragment();

                // 添加新fragment时必须用前面获得的tag，这点很重要
                ft.add(container.getId(), fragment, fragmentTag);
                ft.attach(fragment);
                ft.commitAllowingStateLoss();
            } else if (flag && position == 1) {
                // 如果这个fragment需要更新
                FragmentTransaction ft = fm.beginTransaction();
                // 移除旧的fragment
                ft.remove(fragment);
                // 换成新的fragment，定义成你想要的新的fragment
                fragment = new TelephoneLoginFragment();

                // 添加新fragment时必须用前面获得的tag，这点很重要Fs
                ft.add(container.getId(), fragment, fragmentTag);
                ft.attach(fragment);
                ft.commitAllowingStateLoss();
            }
            return fragment;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (index==0){
            leftFramgnet.onActivityResult(requestCode,resultCode,data);
        }else if(index==1){
            rightFramgnet.onActivityResult(requestCode,resultCode,data);
        }
    }
}
